package com.sihuatech.coreagent.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.util.StringUtil;
import com.google.common.collect.Maps;
import com.sihuatech.coreagent.controller.CodeRelationshipController;
import com.sihuatech.coreagent.core.communication.entity.RequestSetting;
import com.sihuatech.coreagent.core.communication.entity.Response;
import com.sihuatech.coreagent.mapper.ProvinceMapper;
import com.sihuatech.coreagent.mapper.RequestSettingMapper;
import com.sihuatech.coreagent.redis.RedisManager;

@Service
public class RequestSettingService {
	
	private static final Logger logger = LoggerFactory.getLogger(RequestSettingService.class);
	
	@Autowired
	private RequestSettingMapper requestSettingMapper;
	@Autowired
	private RedisManager redisManager;
	/**
	 * 保存请求配置
	 * @param requestSetting
	 * @param res
	 * @return
	 */
	public Response saveRequestSetting(RequestSetting requestSetting,Response res) {
		//必填项校验
		if(!checkRequestSetting(requestSetting)) {
			res.failWithMessage("缺少必填项");
		}
		//入库保存
		if (requestSetting.getId() == null) {
			if (checkExit(requestSetting)) {
				res.failWithMessage("不可重复创建");
				return res;
			}
			requestSettingMapper.insert(requestSetting);
		}else {
			requestSettingMapper.updateById(requestSetting);
		}
		logger.info("请求参数配置信息入库成功：" + requestSetting.toString());
		res.success();
		try {
			redisManager.addInfo(requestSetting.getInterfaceType(), requestSetting.getProvinceCode(), requestSetting.getPlatformCode(), requestSetting);
		}catch (Exception e) {
			logger.error("请求参数配置信息入redis异常");
		}
		
		return res;
	}
	
	public boolean checkExit(RequestSetting rs) {
		Map<String, Object> columnMap = Maps.newHashMap();
		columnMap.put("interface_type", rs.getInterfaceType());
		columnMap.put("platform_code", rs.getPlatformCode());
		columnMap.put("province_code", rs.getProvinceCode());
		List<RequestSetting> list = requestSettingMapper.selectByMap(columnMap);
		if(list != null && list.size() > 0) {
			return true;
		}
		return false;
	}
	
	public boolean checkRequestSetting(RequestSetting requestSetting) {
		if(StringUtils.isEmpty(requestSetting.getPlatformCode())) {
			return false;
		}
		if(StringUtils.isEmpty(requestSetting.getPlatformUrl())) {
			return false;
		}
		if(StringUtils.isEmpty(requestSetting.getProvinceCode())) {
			return false;
		}
		if(StringUtils.isEmpty(requestSetting.getInterfaceType())) {
			return false;
		}
		if(requestSetting.getRetryTimes() == null) {
			return false;
		}
		if(StringUtils.isEmpty(requestSetting.getOvertime())) {
			return false;
		}
		return true;
	}
	
	public Response updateRequestSetting(RequestSetting requestSetting, Response res) {
		// 必填项校验
		if (!checkRequestSetting(requestSetting)) {
			res.failWithMessage("缺少必填项");
		}
		requestSettingMapper.updateById(requestSetting);
		res.success();
		return res;
	}
	
	public RequestSetting getById(Long id) {
		return requestSettingMapper.selectById(id);
	}
	
	public boolean deleteByIds(String[] ids) {
		try {
			for (String id : ids) {
				RequestSetting requestSetting = requestSettingMapper.selectById(id);
				logger.info("删除id为" + id + ",的请求参数配置信息," + "具体信息为" + requestSetting.toString());
				requestSettingMapper.deleteById(id);
				redisManager.deleteInfo(requestSetting.getInterfaceType() + requestSetting.getProvinceCode() + requestSetting.getPlatformCode());
			}
		}catch (Exception e) {
			logger.info("删除异常");
			return false;
		}
		return true;
	}
}
